package com.util;


import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;

public class DBHelper {

    public Connection getCon() {
        Connection con = null;
        try {
            Properties properties = new Properties();
            InputStream is = DBHelper.class.getClassLoader().getResourceAsStream("com/util/jdbc.properties");
            properties.load(is);
            Class.forName(properties.getProperty("mysql.driver"));
            String url = properties.getProperty("mysql.url");
            String username = properties.getProperty("mysql.username");
            String password = properties.getProperty("mysql.password");

            con = DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            //e.printStackTrace();
            System.out.println("mysql 驱动类地址不对，没有导入jar，url路径不对。");
        } catch (SQLException e) {
            e.printStackTrace();
        }catch (NullPointerException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return con;
    }

    public void closeAll(Connection con, PreparedStatement ps, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public ArrayList selectinfo(String sql,Class cla,Object...objs){
        ArrayList list=new ArrayList();
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            con = getCon();
            ps = con.prepareStatement(sql);
            for (int i = 0; i < objs.length; i++) {
                ps.setObject(i + 1, objs[i]);
            }
            rs = ps.executeQuery();
            while (rs.next()) {
                //1.根据cla，获得字节码对应类型下的对象：
                Object o = cla.newInstance();
                //2.给对象属性赋值：
                Field[] declaredField = cla.getDeclaredFields();
                for (Field f : declaredField) {
                    f.setAccessible(true);
                    f.set(o, rs.getObject(f.getName()));
                }
                list.add(o);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NullPointerException e){
            e.printStackTrace();
        }
        finally {
            closeAll(con, ps, rs);
        }
        return list;
    }



    public int updateTable(String sql, Object... objs) {
        Connection con = null;
        PreparedStatement ps = null;
        int count = 0;
        try {
            //1+2
            con = getCon();
            //4.获得执行对象
            ps = con.prepareStatement(sql);
            for (int i = 0; i < objs.length; i++) {
                ps.setObject(i + 1, objs[i]);
            }
            //5.执行sql 获得结果
            count = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(con, ps, null);
        }
        //6.忽略
        //7.关闭
        return count;
    }
    public ArrayList query(String sql,Class c,Object... objs){
        ArrayList list=new ArrayList();
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            //1+2
            con = getCon();
            //4.获得执行对象
            ps = con.prepareStatement(sql);
            for (int i = 0; i < objs.length; i++) {
                ps.setObject(i + 1, objs[i]);
            }
            //5.执行sql，获得结果
            rs = ps.executeQuery();//executeQuery()处理查询sql命令
            //6.处理结果：
            while(rs.next()){
                Object obj= c.newInstance();
                Field[] fs = c.getDeclaredFields();
                for (Field f:fs) {
                    f.setAccessible(true);//暴力授权；
                    f.set(obj,rs.getObject(f.getName()));
                }
                list.add(obj);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } finally {
            //7.关闭
            closeAll(con, ps, rs);
        }
        return list;
    }
    public int query(String sql,Object... objs){
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        int count=0;
        try {
            //1+2
            con = getCon();
            //4.获得执行对象
            ps = con.prepareStatement(sql);
            for (int i = 0; i < objs.length; i++) {
                ps.setObject(i + 1, objs[i]);
            }
            //5.执行sql，获得结果
            rs = ps.executeQuery();//executeQuery()处理查询sql命令
            //6.处理结果：
            if(rs.next()){
                count=rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }  finally {
            //7.关闭
            closeAll(con, ps, rs);
        }
        return count;
    }
}
